<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="创建人ID" prop="createUserId">
        <el-input v-model="dataForm.createUserId" placeholder="创建人ID"></el-input>
      </el-form-item>
      <el-form-item label="订单号" prop="no">
        <el-input v-model="dataForm.no" placeholder="订单号"></el-input>
      </el-form-item>
      <el-form-item label="支付流水号" prop="payNo">
        <el-input v-model="dataForm.payNo" placeholder="支付流水号"></el-input>
      </el-form-item>
      <el-form-item label="充值金额" prop="amount">
        <el-input v-model="dataForm.amount" placeholder="充值金额"></el-input>
      </el-form-item>
      <el-form-item label="充值时长" prop="time">
        <el-input v-model="dataForm.time" placeholder="充值时长"></el-input>
      </el-form-item>
      <el-form-item label="订单状态：0->待付款；1->已付款；2->已关闭；3->无效订单" prop="status">
        <el-input v-model="dataForm.status" placeholder="订单状态：0->待付款；1->已付款；2->已关闭；3->无效订单"></el-input>
      </el-form-item>
      <el-form-item label="支付方式：0->未支付；1->支付宝；2->微信" prop="payType">
        <el-input v-model="dataForm.payType" placeholder="支付方式：0->未支付；1->支付宝；2->微信"></el-input>
      </el-form-item>
      <el-form-item label="支付时间" prop="paymentTime">
        <el-input v-model="dataForm.paymentTime" placeholder="支付时间"></el-input>
      </el-form-item>
      <el-form-item label="支付宝支付链接" prop="alipayUrl">
        <el-input v-model="dataForm.alipayUrl" placeholder="支付宝支付链接"></el-input>
      </el-form-item>
      <el-form-item label="微信支付链接" prop="wxpayUrl">
        <el-input v-model="dataForm.wxpayUrl" placeholder="微信支付链接"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          createTime: '',
          deleted: '',
          version: '',
          lastModifyTime: '',
          createUserId: '',
          no: '',
          payNo: '',
          amount: '',
          time: '',
          status: '',
          payType: '',
          paymentTime: '',
          alipayUrl: '',
          wxpayUrl: ''
        },
        dataRule: {
          createUserId: [
            {required: true, message: '创建人ID不能为空', trigger: 'blur'}
          ], no: [
            {required: true, message: '订单号不能为空', trigger: 'blur'}
          ], payNo: [
            {required: true, message: '支付流水号不能为空', trigger: 'blur'}
          ], amount: [
            {required: true, message: '充值金额不能为空', trigger: 'blur'}
          ], time: [
            {required: true, message: '充值时长不能为空', trigger: 'blur'}
          ], status: [
            {required: true, message: '订单状态：0->待付款；1->已付款；2->已关闭；3->无效订单不能为空', trigger: 'blur'}
          ], payType: [
            {required: true, message: '支付方式：0->未支付；1->支付宝；2->微信不能为空', trigger: 'blur'}
          ], paymentTime: [
            {required: true, message: '支付时间不能为空', trigger: 'blur'}
          ], alipayUrl: [
            {required: true, message: '支付宝支付链接不能为空', trigger: 'blur'}
          ], wxpayUrl: [
            {required: true, message: '微信支付链接不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/manage/order/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 200) {
                Object.assign(this.dataForm, data.data)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manage/order/${!this.dataForm.id ? 'save' : 'update'}`),
              method: `${!this.dataForm.id ? 'post' : 'put'}`,
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'createUserId': this.dataForm.createUserId,
                'no': this.dataForm.no,
                'payNo': this.dataForm.payNo,
                'amount': this.dataForm.amount,
                'time': this.dataForm.time,
                'status': this.dataForm.status,
                'payType': this.dataForm.payType,
                'paymentTime': this.dataForm.paymentTime,
                'alipayUrl': this.dataForm.alipayUrl,
                'wxpayUrl': this.dataForm.wxpayUrl
              })
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
